package net.csdn.business.discuss.api.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.facade.DiscussWrapperFacade;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.model.query.DisciussDetailQuery;
import net.csdn.business.discuss.api.model.query.DiscussConfigQuery;
import net.csdn.business.discuss.api.model.query.DiscussQuery;
import net.csdn.business.discuss.api.model.vo.AnswerSortVO;
import net.csdn.business.discuss.api.model.vo.DiscussVO;
import net.csdn.business.discuss.api.service.IDiscussAnswerRecordService;
import net.csdn.business.discuss.api.service.IDiscussService;
import net.csdn.business.discuss.common.enums.ResultCodeEnum;
import net.csdn.business.discuss.common.exception.BusinessException;
import net.csdn.business.discuss.common.model.vo.UserInfoVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @ClassName DiscussController
 * @Description 讨论Controller
 * @Author zhangyl
 * @Date 2023/7/18 11:35 
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/discuss")
public class DiscussController
{
    @Autowired
    private DiscussWrapperFacade discussWrapperFacade;

    @Autowired
    private IDiscussAnswerRecordService discussAnswerRecordService;

    @Autowired
    private IDiscussService discussService;


    /**
     * 讨论打开时，相关资源初始化
     */
    @PostMapping("/init")
    public Integer initDiscuss(@RequestBody @Validated InitDiscussDTO params)
    {
        return discussService.initDiscuss(params);
    }



    /**
     * 查询讨论是否显示
     */
    @PostMapping("/isShow")
    public Map<String,Boolean> isShowDiscuss(@RequestBody @Validated DiscussConfigQuery params)
    {
        return discussService.isShowDiscuss(params);
    }


    /**
     * 查询讨论总数
     */
    @GetMapping("/total/{sourceId}/{sourceType}")
    public Integer getTotal(@PathVariable("sourceId")String sourceId,@PathVariable("sourceType")Integer sourceType)
    {
        return discussService.getTotal(sourceId,sourceType);
    }


    /**
     * 讨论、评论、回复点赞
     */
    @PostMapping("/like")
    public Integer like(@RequestBody @Validated DiscussLikeDTO params)
    {
        return discussService.like(params);
    }


    /**
     * 讨论、评论、回复取消点赞
     */
    @PostMapping("/unlike")
    public Integer unlike(@RequestBody @Validated DiscussLikeDTO params)
    {
        return discussService.unLike(params);
    }

    /**
     * 讨论打开或关闭
     */
    @PostMapping("/set")
    public Integer discussSet(@RequestBody @Validated UpdateDiscussConfigDTO params)
    {
        return discussService.discussSet(params);
    }


    /**
     * 分页查询讨论列表
     */
    @PostMapping("/page")
    public Page<DiscussVO> findPageList(@RequestBody @Validated DiscussQuery query) throws Exception
    {
        return discussService.findPageList(query);
    }

    /**
     * 根据id获取讨论详细信息
     */
    @PostMapping(value = "/detail")
    public DiscussVO getInfo(@RequestBody @Validated DisciussDetailQuery query)
    {
        return discussService.selectDiscussDetail(query);
    }


    /**
     * 根据id获取讨论简要信息，不包含用户信息、是否点赞等
     */
    @GetMapping(value = "/{discussId}")
    public DiscussVO getById(@PathVariable("discussId")String discussId){
        return discussService.selectDiscussById(discussId);
    }

    /**
     * 新增讨论
     */
    @PostMapping
    public String add(@RequestBody @Validated NewDiscussDTO discuss)
    {
        return discussWrapperFacade.add(discuss);
    }


    /**
     * 投票
     */
    @PostMapping("/vote")
    public Integer vote(@RequestBody @Validated DiscussVoteDTO params)
    {
        return discussService.vote(params);
    }


    /**
     * 修改讨论标签
     */
    @PostMapping("/label")
    public Integer addLabel(@RequestBody @Validated DiscussLabelDTO params)
    {
        return discussService.addLabel(params);
    }

    /**
     * 修改讨论
     */
    @PutMapping
    public Integer edit(@RequestBody @Validated UpdateDiscussDTO discuss)
    {
        return discussWrapperFacade.edit(discuss);
    }

    /**
     * 更改分类
     */
    @PostMapping("/changeCate")
    public Integer changeCategory(@RequestBody @Validated ChangeCategoryDTO params)
    {
        return discussService.changeCategory(params);
    }


    /**
     * 更改锁定状态
     */
    @PostMapping("/changeLock")
    public Integer changeLock(@RequestBody @Validated ChangeLockDTO params)
    {
        return discussService.changeLock(params);
    }


    /**
     * 更改置顶状态
     */
    @PostMapping("/changePin")
    public Integer changePin(@RequestBody @Validated ChangePinDTO params)
    {
        return discussService.changePin(params);
    }

    /**
     * 更改分类中的置顶状态
     */
    @PostMapping("/changeCatePin")
    public Integer changeCategoryPin(@RequestBody @Validated ChangePinDTO params)
    {
        return discussService.changeCategoryPin(params);
    }

    /**
     * 更改讨论状态：打开或者关闭
     */
    @PostMapping("/changeState")
    public Integer changeState(@RequestBody @Validated ChangeStateDTO params)
    {
        return discussService.changeState(params);
    }


    /**
     * 转移讨论
     */
    @PostMapping("/transfer")
    public Integer transfer(@RequestBody @Validated TransferDTO params)
    {
        return discussService.transfer(params);
    }


    /**
     * 删除讨论
     */
	@DeleteMapping
    public Integer remove(@RequestBody @Validated DeleteResourceDTO params)
    {
        return discussWrapperFacade.remove(params);
    }

    /**
     * 查询discuss关联的用户列表
     */
    @GetMapping("/relateUser/{id}")
    public Set<UserInfoVO> relateUser(@PathVariable("id") String id)
    {
        return discussService.relateUser(id);
    }

    /**
     * 查询discuss最近活动的用户列表
     */
    @GetMapping("/recentActivity/{id}")
    public List<UserInfoVO> recentActivity(@PathVariable("id") String id)
    {
        return discussService.recentActivity(id);
    }


    /**
     * 更新审核状态
     */
    @PostMapping("/updateAuditStatus")
    public Integer updateAuditStatus(@RequestBody @Validated UpdateAuditStatusDTO params){
        log.info("discuss update audit status params:{}", JSON.toJSONString(params));
        return discussService.updateAuditStatus(params);
    }

    /**
     * 查询最近30天回复的排行榜
     */
    @GetMapping("/answerSort/{sourceId}/{sourceType}")
    public List<AnswerSortVO> getAnswerSort(@PathVariable("sourceId")String sourceId,@PathVariable("sourceType")Integer sourceType){
        return discussAnswerRecordService.getAnswerSort(sourceId,sourceType);
    }



    /**
     * 我创建的讨论列表
     */
    @PostMapping("/my/page")
    public Page<DiscussVO> myDiscussPageList(@RequestBody @Validated DiscussQuery query) throws Exception
    {
        return discussService.myDiscussPageList(query);
    }


    /**
     * 我参与的讨论列表
     */
    @PostMapping("/join/page")
    public Page<DiscussVO> joinDiscussPageList(@RequestBody @Validated DiscussQuery query) throws Exception
    {
        return discussService.joinDiscussPageList(query);
    }


    /**
     * 组织或仓库删除的时候，同步删除相关讨论和评论,内部接口
     */
    @DeleteMapping("/sync_delete")
    public void syncDelete(@RequestBody @Validated SyncDeleteDTO params) throws Exception
    {
         discussService.syncDelete(params);
    }


}
